$(function(){
  $('input').change(function(){
  /*  alert(1);
    console.log($(this));*/
  });
});

var mas = new Array();
var icon;
var lock = false;
function saveToken(key) {    
    var str =  $("input[name="+key+"]");
           
    lockDisplay(0.2, 700);
                   
    var obj = new Object();  
    var objKey = new Object();
    for(i=0; i<str.length; i++) {      
      obj[str[i].id] = str[i].value;            
    }          
    objKey = key;
    var langObj = {tocken : key};
    var JSONOObject = [key, obj, $('#blockID').html()];    
    var JSONOString = JSON.stringify(JSONOObject);
    
    AJAXRequest(
        'iJLanguages', 
        'saveTockens', 
        JSONOString, 
        
        function log(data) {
            unlockDisplay(400, data);            
        },
        
        function log(data) {
            unlockDisplay(400);
            showMessageOpt('fail', 'Fail', 'something was wrong'); 
        }
    );
}
function addTocken(arrayToken, save, trash, lang_icon) {
    if(lock) return;
    lock = true;
    $('#insert').removeClass('green');
    $('#insert').addClass('gray');
    icon = lang_icon;
    mas = arrayToken.split(';');    
    $newrow  = "<tr><td id = 'replace' style='padding: 2px 6px 2px 6px' align='center'><input id='newToken' onclick='clearText()' style='width:100%' value='New Tocken'/></td>";
    for(i=0;i<mas.length-1;i++)
      $newrow = $newrow + "<td style='padding: 2px 6px 2px 6px' align='center'><input id='" + mas[i] + "' style='width:100%'/></td>";
    $newrow = $newrow + "<td align='right'> <span onclick='newToken()'>"+ save +" &nbsp; </span>" + trash + "&nbsp;</td> </tr>";      
    $tableHtml = $('.dataTable tbody').html();
    $('.dataTable tbody').html($newrow + $tableHtml);   
    $('#steps').height($('#steps').height() + 30);     
}

function clearText() {
  $('#newToken').val('');
}

function newToken(blokID) {
    var token = $('#newToken').val();
    var tokenValue = new Object();
    var lang = new Object();
    lockDisplay(0.2, 700);
    
    for(i=0;i<mas.length-1; i++) {
      tokenValue[mas[i]] = $("#"+mas[i]).val();            
    }           
    
    var JSONOObject = [token, tokenValue, $('#blockID').html(),$('#page').html()];    
    var JSONOString = JSON.stringify(JSONOObject);
    
    AJAXRequest(
        'iJLanguages', 
        'newTockens', 
        JSONOString, 
        
        function log(data) {
            finishAdded(data.trash,token,tokenValue);            
            unlockDisplay(400, data.message);   
            $('#insert').removeClass('gray');
            $('#insert').addClass('green');    
            lock = false;     
        },
        
        function log(data) {
           unlockDisplay(400);
           showMessageOpt('fail', 'Fail', 'something was wrong'); 
           $('#insert').removeClass('gray');
           $('#insert').addClass('green');
           lock = false;
        }
    );           
}
function finishAdded(trash,token,tokenValue){
  newrow  = " &nbsp;" + icon+" &nbsp; "+token;   
  $('#replace').html(newrow);      
  $('#replace').attr('align', 'left');
  $('#replace').attr('id', 'added');
}